<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/24 0024
 * Time: 上午 8:43
 */

namespace Home\Controller;



use Think\Controller;

class DataStatisticsController extends Controller
{
    /**
     * 销售统计，销售额统计
     */
    function sales(){
        $this->display();
    }

    /**
     * 处理销售数据
     * order 下单数
     * sum 销售数量
     * amount 销售总金额
     */
    function diagram(){
        $Y =  date('Y');//当前年份 2017
        $m = date('m');//当前月份 04
        $d =  date('d');//当前日期 24
        $w = date('w');//以数字形式标识周（0=Sunday,1=Monday, ...,6=Saturday）

        /**
         * 月份查询，是查询2015年(2017年没有数据)1月到12月所有数据
         */

        $order = array();
            $amount = array();$sum = array();$month = array();
            for ($i=1;$i<=12;$i++){
                if($i<10) {
                    $order[$i] = M()->query("select SUM(orderCount) as count from ssg_order where orderTime like '2015-0" . $i . "%'");
                    $sum[$i] = M()->query("select sum(orderCount) as sum from ssg_order where orderStatus = 1 and orderTime like '2015-0" . $i . "%'");
                    $amount[$i] = M()->query("select sum(orderPrice) as amount from ssg_order where orderTime like '2015-0".$i."%'");
                }else{
                    $order[$i] = M()->query("select SUM(orderCount) as count from ssg_order where orderTime like '2015-" . $i . "%'");
                    $sum[$i] = M()->query("select sum(orderCount) as sum from ssg_order where orderStatus = 1 and orderTime like '2015-" . $i . "%'");
                    $amount[$i] = M()->query("select sum(orderPrice) as amount from ssg_order where orderTime like '2015-".$i."%'");
                }
                $month['order'][$i] = $order[$i][0]['count'];//下单数
                $month['sum'][$i] = $sum[$i][0]['sum'];//销售数量
                $month['amount'][$i] = $amount[$i][0]['amount']/100;//销售总金额  ，  单位百元
            }
            $json = json_encode($month);

        /**
         * 周
         * */
        if($_GET['week'] == 'week'){
            $week = array();
            $time = strtotime('2015-'.$m.'-'.$d);//当前日期的时间戳
            $ub_time = $time+86400;
            for ($i=1;$i<=5;$i++){
                //时间处理
                $ue_time = $ub_time-86400;
                $ub_time = $ub_time-86400*7;
                $e_time = date('Y-m-d',$ue_time);
                $b_time = date('Y-m-d',$ub_time);
                    $order[$i] = M()->query("select SUM(orderCount) as count from ssg_order where orderTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && orderTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");
                    $sum[$i] = M()->query("select sum(orderCount) as sum from ssg_order where orderStatus = 1 and orderTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && orderTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");
                    $amount[$i] = M()->query("select sum(orderPrice) as amount from ssg_order where orderTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && orderTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");

                $week['order'][$i] = $order[$i][0]['count'];//下单数
                $week['sum'][$i] = $sum[$i][0]['sum'];//销售数量
                $week['amount'][$i] = $amount[$i][0]['amount']/100;//销售总金额  ，  单位百元
            }
            unset($json);
            $json = json_encode($week);
        }


        /**
         * 天
         */

        if($_GET['day'] == 'day'){
            $day = array();
            $time = strtotime('2015-'.$m.'-'.$d);//当前日期的时间戳
            $ub_time = $time+86400;
            for ($i=1;$i<$w+1;$i++){
                //时间处理
                $ue_time = $ub_time-86400;
                $ub_time = $ue_time-86400;
                $e_time = date('Y-m-d',$ue_time);
                $b_time = date('Y-m-d',$ub_time);
                    $order[$i] = M()->query("select SUM(orderCount) as count from ssg_order where orderTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && orderTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");
                    $sum[$i] = M()->query("select sum(orderCount) as sum from ssg_order where orderStatus = 1 and orderTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && orderTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");
                    $amount[$i] = M()->query("select sum(orderPrice) as amount from ssg_order where orderTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && orderTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");

                $day['order'][$i] = $order[$i][0]['count'];//下单数
                $day['sum'][$i] = $sum[$i][0]['sum'];//销售数量
                $day['amount'][$i] = $amount[$i][0]['amount']/100;//销售总金额  ，  单位百元
            }
            unset($json);
            $json = json_encode($day);
        }

            echo $json;
    }


    /**
     * 用户数统计
     */
    function User(){
        $this->display();
    }

    /**
     * 处理用户数数据
     */
    function userNum(){
        $Y =  date('Y');//当前年份 2017
        $m = date('m');//当前月份 04
        $d =  date('d');//当前日期 24
        $time = strtotime($Y.'-'.$m.'-'.$d);//当前日期的时间戳
        $ub_time = $time+86400;

        $result = array();

        for ($i=1;$i<=5;$i++) {
            //时间处理
            $ue_time = $ub_time - 86400;
            $ub_time = $ub_time - 86400 * 7;
            $e_time = date('Y-m-d', $ue_time);
            $b_time = date('Y-m-d', $ub_time);
            $count[$i] = M()->query("select count(id) as count from ssg_account where registerTime > STR_TO_DATE('".$b_time."','%Y-%m-%d') && registerTime <= STR_TO_DATE('".$e_time."','%Y-%m-%d')");

            $result[$i] = $count[$i][0]['count'];//新增用户数量
        }
        array_push($result,date('Y-m-d'));//添加一个当前时间
        $json = json_encode($result);
        echo $json;
    }

}
